package com.jmlee.controller;

import com.jmlee.bo.SubmitOrderBO;
import com.jmlee.exception.Asserts;
import com.jmlee.pojo.CommonResult;
import com.jmlee.service.OrderService;
import com.jmlee.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Descriptio 订单服务接口
 * @Author jmLee
 * @Date 2021/3/10 17:03
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 新增订单
     *
     * @return
     */
    @PostMapping("/create")
    public CommonResult createOrder(@RequestBody SubmitOrderBO submitOrderBO) {

        return orderService.createOrder(submitOrderBO);
    }


    /**
     * 获取特定状态的订单列表
     *
     * @param userId
     * @param orderStatus+
     * @return
     */
    @GetMapping("/list")
    public CommonResult getOrder(Long userId, Integer orderStatus) {

        List<OrderVO> userOrders = orderService.getUserOrders(userId, orderStatus);

        return CommonResult.success(userOrders);
    }

    /**
     * 查询某个未支付订单
     *
     * @param orderId
     * @return
     */
    @GetMapping("/unPay")
    public CommonResult unPayOrder(Long orderId) {

        if (orderId == null) {
            Asserts.fail("请求参数不能为空");
        }

        OrderVO unPayOrder = orderService.getUnpayOrder(orderId);
        if(unPayOrder == null) {
            Asserts.fail("订单已过期");
        }

        return CommonResult.success(unPayOrder);
    }

}
